package com.kaibes.imserver.model.utils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.kaibes.imserver.model.ClientStringWriter;

public class JsonParserUtil {

	private JSONObject jsonObject;
	private JSONObject jsonObjectSon;
	private JSONArray jsonArraySon;

	public JsonParserUtil(String json) {
		jsonObject = new JSONObject(json);
	}

	public JsonParserUtil() {
		jsonObject = new JSONObject();
	}

	public JsonParserUtil(int head, int action) {
		jsonObject = new JSONObject();

		putValue("h", head).putValue("a", action).newJsonObjectSon()
				.newJsonArraySon();
	}

	public JsonParserUtil(String filename, String data) {
		jsonObject = new JSONObject();
		putValue("h", 0).putValue("a", filename).putValue("d", data);
	}

	public JsonParserUtil newSelf() {
		jsonObject = new JSONObject();
		return this;
	}

	public JsonParserUtil newSelf(int head, int action) {
		newSelf().putValue("h", head).putValue("a", action).newJsonObjectSon()
				.newJsonArraySon();
		return this;
	}
	
	public JsonParserUtil newSelf(int head, int action, String message) {
		newSelf().putValue("h", head).putValue("a", action).newJsonObjectSon()
				.newJsonArraySon().putJsonObjectSonValue("message", message).addJSONObjectSon();
		return this;
	}
	
	public JsonParserUtil newSelf(String filename, String data) {
		newSelf().putValue("h", 0).putValue("a", filename).putValue("d", data);
		return this;
	}

	public JsonParserUtil newJsonObjectSon() {
		jsonObjectSon = new JSONObject();
		return this;
	}

	public JsonParserUtil newJsonArraySon() {
		jsonArraySon = new JSONArray();
		return this;
	}

	public JsonParserUtil putValue(String name, Object value) {
		jsonObject.put(name, value);
		return this;
	}

	public JsonParserUtil putJsonObjectSonValue(String name, Object value) {
		jsonObjectSon.put(name, value);
		return this;
	}

	public JsonParserUtil putJsonArraySonValue(Object value) {
		jsonArraySon.put(value);
		return this;
	}

	public JsonParserUtil parserJsonString(String json) {
		try {
			jsonObject = new JSONObject(json);
		} catch (JSONException e) {
			return null;
		}
		return this;
	}

	public JsonParserUtil addJSONObjectSon() {
		jsonArraySon.put(jsonObjectSon);
		return this;
	}

	public JsonParserUtil addJsonArraySon(String name) {
		jsonObject.put(name, jsonArraySon);
		return this;
	}

	public Object getValue(String name) {
		try {
			return jsonObject.get(name);
		} catch (JSONException e) {
			return null;
		}
	}

	public JsonParserUtil setJsonArraySon(String name) {
		try {
			jsonArraySon = jsonObject.getJSONArray(name);
			if (jsonArraySon.length() > 0) {
				setJsonObjectSon(0);
				return this;
			}
		} catch (JSONException e) {
			//TODO
		}
		return null;
	}

	public JSONArray getJsonArraySon() {
		return jsonArraySon;
	}

	public JsonParserUtil setJsonObjectSon(int index) {
		jsonObjectSon = (JSONObject) jsonArraySon.get(index);
		return this;
	}

	public JSONObject getJsonObjectSon() {
		return jsonObjectSon;
	}

	public Object getJsonObjectSonValue(String name) {
		if (jsonObjectSon != null) {
			try {
				return jsonObjectSon.get(name);
			} catch (JSONException e) {
				return null;
			}
		}
		return null;
	}

	public JsonParserUtil removeValue(String name) {
		jsonObject.remove(name);
		return this;
	}

	public void removeArrayValue(int index) {
		jsonArraySon.remove(index);
	}

	public String toString() {
		return jsonObject.toString();
	}

	public void commit(ClientStringWriter clientStringWriter) {
		addJSONObjectSon();
		commitNotJSONObjectSon(clientStringWriter);
	}
	
	public void commitNotJSONObjectSon(ClientStringWriter clientStringWriter) {
		addJsonArraySon("d");
		commitNotJsonArraySon(clientStringWriter);
	}
	
	public void commitNotJsonArraySon(ClientStringWriter clientStringWriter) {
		clientStringWriter.sendDataln(toString());
	}
	
	public int length(){
		return jsonObject.length();
	}
	
}
